package com.atguigu.spzx.manager.controller.system;

import com.atguigu.spzx.manager.service.system.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询出角色管理表
     * @param pageNum
     * @param pageSize
     * @param sysRoleDto
     * @return
     */
    @RequestMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@PathVariable("pageNum") Integer pageNum,
                                                @PathVariable("pageSize") Integer pageSize, SysRoleDto sysRoleDto){

        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(pageNum,pageSize,sysRoleDto);

        return Result.ok(pageInfo);
    }

    /**
     * 添加角色
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/saveSysRole")
    public Result saveRole(@RequestBody SysRoleDto sysRoleDto){
        sysRoleService.saveRole(sysRoleDto);
        return Result.ok();
    }

    /**
     * 修改角色
     * @param sysRoleDto
     * @return
     */
    @PutMapping("/updateSysRole")
    public Result updateRole(@RequestBody SysRoleDto sysRoleDto){
        sysRoleService.updateRole(sysRoleDto);
        return Result.ok();
    }

    /**
     * 根据id删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/deleteSysRoleById/{roleId}")
    public Result deleteSysRoleById(@PathVariable ("roleId") Long roleId){
        sysRoleService.deleteSysRoleById(roleId);
        return Result.ok();
    }


    /**
     * 查询所有角色和当前用户已经拥有的角色
     * @param userId
     * @return
     */
    @GetMapping("/getAllRolesAndRoleIdByUserId/{userId}")
    public Result getAllRolesAndRoleIdByUserId(@PathVariable("userId") Long userId){
        Map<String,Object> resultMap = sysRoleService.getAllRolesAndRoleIdByUserId(userId);
        return Result.ok(resultMap);
    }

}
